//
//  HMSQliteTools.swift
//  WeiBoSwift
//
//  Created by hgj on 2016/11/27.
//  Copyright © 2016年 HuGuoJian. All rights reserved.
//

import UIKit

class HMSQliteTools: NSObject {

    
    static let shared: HMSQliteTools = HMSQliteTools()
    
    let queue: FMDatabaseQueue
    
    override init() {
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("weibo.db")
        
        queue = FMDatabaseQueue(path: path)
        
        super.init()
        
        createTable()
    }
    
    private func createTable() {
        
        let sql = "CREATE TABLE IF NOT EXISTS T_Status (statusId INTEGER PRIMARY KEY NOT NULL, status TEXT, userId TEXT, create_date TEXT DEFAULT (datetime('now','localtime')));"
        
        queue.inTransaction { (db, rollback) in
            let res = db!.executeStatements(sql)
            
            if res {
                print("建表成功")
            } else {
                print("建表失败")
                
            rollback?.pointee = true
        }
    }
}
}
